﻿Public Class frmMonHoc

    Private Sub frmMonHoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LayDSmh()
    End Sub

    Private Sub LayDSmh()
        Try
            Dim mhBUS As New MonHocBUS
            Dim ds As New List(Of MonHocDTO)
            Dim i As Integer = 1

            ds = mhBUS.LayDanhSachMonHoc()
            dgMonHoc.Rows.Clear()

            For Each mh As MonHocDTO In ds
                dgMonHoc.Rows.Add(i, mh.MaMonHoc, mh.TenMonHoc)
                i += 1
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


    Private Sub btThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThem.Click
        Try
            If (txtMaMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Mon Hoc.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtTenMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ten Mon Hoc.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim mhDTO As New MonHocDTO()
            Dim mhBUS As New MonHocBUS()
            Dim kq As Integer = -1

            mhDTO.MaMonHoc = txtMaMH.Text.Trim()
            mhDTO.TenMonHoc = txtTenMH.Text.Trim()

            kq = mhBUS.ThemMonHoc(mhDTO)

            If kq > 0 Then
                LayDSmh()
            Else
                MessageBox.Show("Them that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            If (ex.Message.IndexOf("create duplicate values in the index, primary key, or relationship") > 0) Then
                MessageBox.Show("Ma Mon Hoc da ton tai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End Try
    End Sub

    Private Sub btXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoa.Click
        Try
            If (txtMaMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Mon Hoc muon Xoa.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim mhDTO As New MonHocDTO()
            Dim mhBUS As New MonHocBUS()
            Dim kq As Integer = -1

            mhDTO.MaMonHoc = txtMaMH.Text.Trim()
            kq = mhBUS.XoaMonHoc(mhDTO)

            If kq > 0 Then
                LayDSmh()
            Else
                MessageBox.Show("Xoa that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btCapNhat.Click
        Try
            If (txtMaMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ma Mon Hoc.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            ElseIf (txtTenMH.Text.Trim.Length = 0) Then
                MessageBox.Show("Nhap Ten Mon Hoc.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Return
            End If

            Dim mhDTO As New MonHocDTO()
            Dim mhBUS As New MonHocBUS()
            Dim kq As Integer = -1

            mhDTO.MaMonHoc = txtMaMH.Text.Trim()
            mhDTO.TenMonHoc = txtTenMH.Text.Trim()

            kq = mhBUS.SuaMonHoc(mhDTO)

            If kq > 0 Then
                LayDSmh()
            Else
                MessageBox.Show("Cap Nhat that bai.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btTim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTim.Click
        Try
            Dim mhDTO As New MonHocDTO()
            Dim mhBUS As New MonHocBUS()
            Dim kq As Integer = -1

            mhDTO.MaMonHoc = txtMaMH.Text.Trim()
            mhDTO.TenMonHoc = txtTenMH.Text.Trim()

            Dim ds As New List(Of MonHocDTO)
            Dim i As Integer = 1

            ds = mhBUS.TraCuuMonHoc(mhDTO)
            dgMonHoc.Rows.Clear()

            For Each mh As MonHocDTO In ds
                dgMonHoc.Rows.Add(i, mh.MaMonHoc, mh.TenMonHoc)
                i += 1
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btXoaForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btXoaForm.Click
        txtMaMH.Text = ""
        txtTenMH.Text = ""
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        Me.Close()
    End Sub

    Private Sub dgMonHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgMonHoc.SelectionChanged
        Try
            If (dgMonHoc.SelectedRows.Count > 0) Then
                If (dgMonHoc.SelectedRows(0).Cells(1).Value = Nothing) Then
                    Return
                End If

                txtMaMH.Text = dgMonHoc.SelectedRows(0).Cells(1).Value.ToString().Trim()
                txtTenMH.Text = dgMonHoc.SelectedRows(0).Cells(2).Value.ToString().Trim()
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class